package com.zxy.hotel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxy.hotel.entity.Order;
import com.zxy.hotel.entity.Room;
import com.zxy.hotel.entity.Vip;
import com.zxy.hotel.mapper.OrderMapper;
import com.zxy.hotel.service.OrderService;
import com.zxy.hotel.service.RoomService;
import com.zxy.hotel.service.VipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jiusi
 * @since 2021-12-12
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Autowired
    private RoomService roomService;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private VipService vipService;

    @Override
    public Integer orderRoom(Integer roomId, String name, String cardId, String phone, String remark) {
        Room room = roomService.getById(roomId);
        if (room == null) {
            return -1;
        }
        if ("有客".equals(room.getStatus())) {
            return -2;
        }
        Order order = new Order();
        order.setRoomId(roomId);
        order.setSdate(LocalDateTime.now());
        order.setEdate(null);
        order.setDiscount(100);
        order.setPrice(null);
        order.setCustomerName(name);
        order.setCustomerCardid(cardId);
        order.setCustomerPhone(phone);
        order.setRemark(remark);
        return orderMapper.insertOne(order);
    }

    @Override
    public Order billRoom(Integer roomId) {
        Order order = orderMapper.selectRoomByRoomId(roomId);
        if (order == null) {
            return null;
        }
        Room room = roomService.getById(roomId);
        order.setEdate(LocalDateTime.now());
        Duration duration = Duration.between(order.getSdate(), order.getEdate());
        long days = (duration.toHours() / 24) + 1;
        BigDecimal price = room.getPrice().multiply(BigDecimal.valueOf(order.getDiscount())).divide(BigDecimal.valueOf(100)).multiply(BigDecimal.valueOf(days));
        order.setPrice(price);
        orderMapper.update(order, new QueryWrapper<Order>().eq("id", order.getId()));
        return getById(order.getId());
    }

    @Override
    public Integer vipOrderRoom(Integer roomId, String cardId, String remark) {
        Room room = roomService.getById(roomId);
        if (room == null) {
            return -1;
        }
        if ("有客".equals(room.getStatus())) {
            return -2;
        }
        Vip vip = vipService.fuzzy(cardId).get(0);
        if (vip == null) {
            return -3;
        }
        Order order = new Order();
        order.setRoomId(roomId);
        order.setSdate(LocalDateTime.now());
        order.setEdate(null);
        order.setDiscount(vip.getDiscount());
        order.setPrice(null);
        order.setCustomerName(vip.getName());
        order.setCustomerCardid(vip.getCardid());
        order.setCustomerPhone(vip.getPhone());
        order.setRemark(remark);
        return orderMapper.insertOne(order);
    }

    @Override
    public Map<String, Integer> getStat() {
        Integer profit = orderMapper.getTodayProfit();
        Integer count = orderMapper.getTodayCount();
        Integer todayNotBillCount = orderMapper.getTodayNotBillCount();
        Integer notBillCount = orderMapper.getNotBillCount();
        Integer total = orderMapper.selectCount(new QueryWrapper<>());

        HashMap<String, Integer> map = new HashMap<>();
        map.put("profit", profit);
        map.put("todayCount", count);
        map.put("todayNotBillCount", todayNotBillCount);
        map.put("notBillCount", notBillCount);
        map.put("total", total);

        return map;
    }

}
